
"""
基础分析模板
"""

from core.utils.llm import LLM
from core.utils.prompt_template_parser import PromptTemplateParser
from json_repair import repair_json
from abc import ABC, abstractmethod
from typing import Union
from pathlib import Path


class BaseAnalyzer(ABC):

    def __init__(self, prompt_file: Union[str, Path]):
        with open(prompt_file, 'r', encoding='utf-8') as fs:
            template_prompt = fs.read()
        self.template_prompt = PromptTemplateParser(template_prompt)
        self.llm = LLM()

    def analyze(self, prompt: str):
        """调用 LLM 进行分析"""
        output = self.llm.generate(prompt)
        think_end_pos = output.find('</think>')
        if think_end_pos != -1:
            output = output[think_end_pos + len('</think>'):]
        data = repair_json(output, return_objects=True)
        return data

    @abstractmethod
    def invoke(self, **kwargs):
        """大模型分析"""
